#pragma once

#include <mutex>
#include "util.hpp"


//实现系统运行配置信息的单例模式
#define CONFIG_FILE "./cloud.conf"
class Config
{
	private:
		Config()
		{
			if(!ReadConfigFile())
				std::cout<<"error"<<std::endl;
		}

		static Config *_instance;
		static std::mutex _mutex;
	private:
		int _hot_time;//热点文件时间
		int _server_port;//端口号
		std::string _server_ip;//IP号
		std::string _download_prefix;//下载前缀
		std::string _packfile_suffix;//压缩文件后缀
		std::string _pack_dir;//压缩文件存放目录
		std::string _back_dir;//备份文件存放目录
		std::string _backup_file;//数据信息存放文件
		bool ReadConfigFile()
		{
			FileUtil fu(CONFIG_FILE);
			std::string body;
			//获取文件内容
			if(fu.GetContent(&body)==false)
			{
				std::cout<<"load config file failed\n";
				return false;
			}
			//对获取到的内容进行反序列化
			Json::Value root;
			if(JsonUtil::UnSerialize(body,&root) == false)
			{
				std::cout<<"parse config file failed\n";
				return false;
			}
			_hot_time=root["hot_time"].asInt();
			_server_port = root["server_port"].asInt();
			_server_ip = root["server_ip"].asString();
			_download_prefix = root["download_prefix"].asString();
			_packfile_suffix = root["packfile_suffix"].asString();
			_pack_dir = root["pack_dir"].asString();
			_back_dir = root["back_dir"].asString();
			_backup_file = root["backup_file"].asString();
			return true;
		}



	public:
		//创建单例
		static Config *GetInstance()
		{
			//检测当前是否有单例对象
			if(_instance==NULL)
			{
				//加锁
				_mutex.lock();
				if(_instance==NULL)
				{
					_instance=new Config();
				}

				//解锁
				_mutex.unlock();

			}
			return _instance;
		}

		int GetHotTime()
		{
			return _hot_time;
		}

		int GetServerPort()
		{
			return _server_port;
		}
		std::string GetServerIp()
		{
			return _server_ip;
		}
		std::string GetDownloadPrefix()
		{
			return _download_prefix;
		}
		std::string GetPackFileSuffix()
		{
			return _packfile_suffix;
		}
		std::string GetPackDir()
		{
			return _pack_dir;
		}
		std::string GetBackDir()
		{
			return _back_dir;
		}
		std::string GetBackUpFile()
		{
			return _backup_file;
		}

};
Config *Config::_instance=NULL;
std::mutex Config::_mutex;

